package com.zxk.app.controller;

import com.zxk.app.dto.TaskMappingDto;
import com.zxk.app.vo.AppStartVo;
import com.zxk.controller.StartController;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author: 舟小亢
 * @date: 2023-11-17 23:17
 */
public class AppController {

    @FXML
    private TextArea shceduleTaskText;

    private static Map<String, String[]> selectTaskMap = new LinkedHashMap() {{
        put("selectTaskB1", new String[]{
                TaskMappingDto.LOGIN_GAME,
                TaskMappingDto.SCRATCH_TICKET,
                TaskMappingDto.SCRATCH_TICKET,
                TaskMappingDto.USE_ENERGY,
                TaskMappingDto.AQ_TASK,
                TaskMappingDto.EXAM_TASK,
                TaskMappingDto.MAP_TASK,
                TaskMappingDto.SECRET_AREA,
                TaskMappingDto.DART_CAR,
                TaskMappingDto.USE_MAP,
                TaskMappingDto.FIGHT_GHOST});
        put("selectTaskB2", new String[]{
                TaskMappingDto.LOGIN_GAME,
                TaskMappingDto.SCRATCH_TICKET,
                TaskMappingDto.SCRATCH_TICKET,
                TaskMappingDto.AQ_TASK,
                TaskMappingDto.EXAM_TASK,
                TaskMappingDto.MAP_TASK,
                TaskMappingDto.SECRET_AREA,
                TaskMappingDto.DART_CAR,
                TaskMappingDto.FIGHT_GHOST});
        put("selectTaskB3", new String[]{
                TaskMappingDto.HARD_INSTANCE_1,
                TaskMappingDto.NORMAL_INSTANCE_1,
                TaskMappingDto.NORMAL_INSTANCE_2,
                TaskMappingDto.FIGHT_GHOST});
        put("selectTaskB4", new String[]{
                TaskMappingDto.SCRATCH_TICKET,
                TaskMappingDto.SCRATCH_TICKET,
                TaskMappingDto.USE_ENERGY,
                TaskMappingDto.AQ_TASK,
                TaskMappingDto.EXAM_TASK,
                TaskMappingDto.MAP_TASK,
                TaskMappingDto.SECRET_AREA,
                TaskMappingDto.DART_CAR,
                TaskMappingDto.FIGHT_GHOST});
        put("selectTaskB5", new String[]{
                TaskMappingDto.SCRATCH_TICKET,
                TaskMappingDto.SCRATCH_TICKET,
                TaskMappingDto.AQ_TASK,
                TaskMappingDto.EXAM_TASK,
                TaskMappingDto.MAP_TASK,
                TaskMappingDto.SECRET_AREA,
                TaskMappingDto.DART_CAR,
                TaskMappingDto.FIGHT_GHOST});
        put("selectTaskB6", new String[]{
                TaskMappingDto.NORMAL_INSTANCE_1,
                TaskMappingDto.NORMAL_INSTANCE_2,
                TaskMappingDto.FIGHT_GHOST});
    }};

    /**
     * 选择任务复选框
     *
     * @param event
     */
    @FXML
    private void handleCheckBoxSelection(MouseEvent event) {
        CheckBox checkBox = (CheckBox) event.getSource();
        // 选中状态就添加到map中
        if (checkBox.isSelected()) {
            AppStartVo.TASK_MAP.put(checkBox.getId(), checkBox.getText());
        } else {
            // 取消状态就从map中移除
            AppStartVo.TASK_MAP.remove(checkBox.getId(), checkBox.getText());
        }
        // 刷新text内容
        StringBuilder sb = new StringBuilder();
        for (String value : AppStartVo.TASK_MAP.values()) {
            sb.append(value);
            sb.append(",");
        }
        shceduleTaskText.setText(sb.toString());
    }

    /**
     * 启动任务按钮
     *
     * @param event
     */
    @FXML
    private void handleStartButton(MouseEvent event) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        if (AppStartVo.TASK_MAP.size() == 0) {
            alert.setTitle("提示");
            alert.setHeaderText(null);
            alert.setContentText("你还未选择任何任务！");
            alert.showAndWait();
            return;
        }
        alert.setTitle("提示");
        alert.setHeaderText(null);
        alert.setContentText("启动成功！");
        alert.showAndWait();
        Button button = (Button) event.getSource();
        button.setDisable(true);
        button.setText("运行中");
        StartController.run();
    }

    /**
     * 一键选择任务按钮
     *
     * @param event
     */
    @FXML
    private void handleSelectTaskButton(MouseEvent event) {
        Button button = (Button) event.getSource();
        String[] taskInfo = selectTaskMap.get(button.getId());
        // 刷新text内容
        StringBuilder sb = new StringBuilder();
        for (String taskName : taskInfo) {
            AppStartVo.TASK_MAP.put(taskName, taskName);
            sb.append(taskName);
            sb.append(",");
        }
        shceduleTaskText.appendText(sb.toString());
    }


    /**
     * 清空任务按钮
     *
     * @param event
     */
    @FXML
    private void handleClearButton(MouseEvent event) {
        AppStartVo.TASK_MAP.clear();
        shceduleTaskText.clear();
    }

}
